window.onload =function(){
    var url="http://192.168.4.18:8000/book";

    function ajax({
        method,
        url,
        success
    }){
        var xhr = new XMLHttpRequest();
        xhr.open(method,url,true);
        xhr.send();
        xhr.onreadystatechange = function(){
            if(this.readyState == 4&&this.status==200){
                var res = JSON.parse(this.responseText);
                success(res);
            }
        }
    }

    function sendRequest(){
        ajax({
            method:"get",
            url:url,
            success: function(res) {
                deal(res);
                // console.log(res);
            }
        })
    }

    function deal(res){
        // console.log(res);
        // console.log(typeof(res));
        res.forEach(element => {
            var{coverImage,title,rating,evaluate,labels,price} = element;
            var msg = `${labels.join("/")}/${price}元`;
            // console.log(msg);
            var html = `
            <div class="bookList">
                <img class="bookImg" src=${coverImage} alt="">
                <div class="msg">
                    <a href=""><p class="title">${title}</p></a>
                    <p class="number"><span class="criticNmb">评分：${rating}</span><span>(${evaluate}人评价)</span></p>
                    <p class="about">${msg}</p>
                </div>
            </div>
            `;
            $(".mainLeft").append(html)
        });

    }
    sendRequest();
}